/**
 * Created by yan on 16/2/24.
 */
$(document).ready(function () {
    $.extend($.fn.validatebox.defaults.rules, {
        Number: {
            validator: function (value, param) {
                var reg = /^[0-9]*$/;
                return reg.test(value);
            },
            message: '请输入数字'
        }
    });

//            $('#tt').treegrid('append', {
//                data: [{"id": 1, "name": "1", "parentId": 1, "remark": "", "state": 1, "type": "system", "url": ""}]
//            });
});

function submitForm() {
    $.messager.progress();	// display the progress bar
    $('#ff').form('submit', {
        url: "/sec/addSecModule",
        onSubmit: function () {
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');	// hide progress bar while the form is invalid
            }
            return isValid;	// return false will stop the form submission
        },
        success: function () {
            $.messager.progress('close');	// hide progress bar while submit successfully
            $('#ff').form('clear');
            $('#dd').dialog('close');
            $('#tg').treegrid('reload');
        },
        onLoadError: function () {
            $.messager.alert("保存信息", "发生错误!", "error");
        }
    });
}

function clearForm() {
    alert('clearForm');
    $('#ff').form('clear');
}

/**
 *格式化状态，如果为1，显示正常，为0显示禁用
 */
function formatStatus(value, row, index) {
    switch (value) {
        case 1:
            return '有效';
        case 0:
            return '无效';
    }
}

function formatParentId(value, row, index) {
    if (value == 0) {
        return '根目录';
    } else {
        return value;
    }
}

var editingId;
var statusData = [{"value": "1", "text": "有效"}, {"value": "0", "text": "无效"}];
var typeData = [{"value": "system", "text": "system"}, {"value": "application", "text": "application"}];
function edit() {
//            alert("1111");
    if (editingId != undefined) {
        $('#tg').treegrid('select', editingId);
        return;
    }
    var row = $('#tg').treegrid('getSelected');
    if (row) {
        editingId = row.id
        $('#tg').treegrid('beginEdit', editingId);
    } else {
        $.messager.alert("提示", "请选择一个目录!", "info");
    }
}

function save() {
    var rowId = "";
    if (editingId != undefined) {
        $('#tg').treegrid('endEdit', editingId);
        rowId = editingId;
        editingId = undefined;
    }

    var row = $('#tg').treegrid('getSelected');
    $.post('/sec/updateSecModule/' + rowId, {
            id: row.id,
            name: row.name,
            type: row.type,
            parentId: row.parentId,
            url: row.url,
            imgUrl: row.imgUrl,
            status: row.status,
            remark: row.remark
        },
        function (resp) {
            if (resp == 'SUCCESS') {
                $('#tg').treegrid('reload');
            } else {
                alert('操作失败!');
            }
        }
    )
    ;
}

function cancel() {
    if (editingId != undefined) {
        $('#tg').treegrid('cancelEdit', editingId);
        editingId = undefined;
    }
}

//        function postData(variables) {
//            var keys = "", values = "", types = "";
//            if (variables) {
//                $.each(variables, function () {
//                    if (keys != "") {
//                        keys += ",";
//                        values += ",";
//                        types += ",";
//                    }
//                    keys += this.key;
//                    values += this.value;
//                    types += this.type;
//                });
//            }
//
//            $.post('/oa/leave/complete/', {
//                keys: keys,
//                values: values,
//                types: types
//            }, function (resp) {
//                if (resp == 'success') {
//                    $('#tg').treegrid('reload');
//                } else {
//                    alert('操作失败!');
//                }
//            });
//        }
